package com.lzq.leetcode.hoot100.ds;


public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
        val = 0;
        next = null;
    }

    public ListNode(int x) {
        val = x;
        next = null;
    }

    public ListNode(int[] arr) {
        // 创建头节点
        this(arr[0]);
        ListNode p = this;
        for (int i=1; i<arr.length; i++) {
            p.next = new ListNode(arr[i]);
            p = p.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        ListNode p = this;
        while (p.next != null) {
            builder.append(p.val + " -> ");
            p = p.next;
        }
        builder.append(p.val);
        return builder.toString();
    }
}